
const A = getApp();
var interval;
Page({
  data: {
    noticeContent:'',
    showNoticeModal:true,
    swiperImg: [],
    everyDayStudy: [],
    newNav: [
      { id: 1, icon: "/images/icon_1001.png", width: 32, height: 32, title: "青年大学习" },
      { id: 2, icon: "/images/icon_1002.png", width: 35, height: 32, title: "志愿服务" },
      { id: 3, icon: "/images/icon_1003.png", width: 32, height: 32, title: "青创协" },
      { id: 4, icon: "/images/icon_1004.png", width: 37, height: 32, title: "生活服务" },
      { id: 5, icon: "/images/icon_1005.png", width: 32, height: 32, title: "招商引资" },
      { id: 6, icon: "/images/icon_1006.png", width: 32, height: 32, title: "团青福利" },
      { id: 7, icon: "/images/icon_1007.png", width: 32, height: 32, title: "团青大本营" },
      { id: 8, icon: "/images/icon_1008.png", width: 32, height: 32, title: "视频学习" }
    ],
    navigate: ['', '', '/pages/index/shop/shop', '/pages/index/shop/shop', '', '/pages/mall/mall', '/pages/index/shop/shop', '/pages/videos/videos'],
    show: false,
    hasSign: false,
    text: [],
    orientation: 'left',//滚动方向
    marqueePace: 1, // 滚动速度
    interval: 20, // 时间间隔
    marqueeDistance: 250,
    marqueeCopy_status: false,
    marqueeMargin: 0,
    noEveryDayStudy: true
  },
  // 跳转功能栏页面
  navgateToDetail(e) {
    let path = A.C(e).id;
    console.log(path);
    console.log(e);
    switch (path) {
      case "1":
        this.getYouthLearn();
        break;
      case "2":
        console.log('志愿服务');
        // wx.navigateTo({
        //   url: '/pages/index/education/education',
        // })
        break;
      case "3":
        console.log('青创协');
        wx.navigateTo({
          url: '/pages/index/shop/shop',
        })
        break;
      case "4":
        console.log('生活服务');
        wx.navigateTo({
          url: '/pages/index/localLives/localLives',
        })
        break;
      case "5":
        console.log('招商引资');
        wx.navigateTo({
            url: '/pages/index/activity/activity',
        })
        break;
      case "6":
        console.log('团青福利');
        if (!wx.getStorageSync('userId')) {
          return wx.navigateTo({ url: '/pages/index/impower/impower', })
        }
        wx.navigateTo({
          url: '/pages/mall/mall',
        })
        break;
      case "7":
        console.log('团青大本营');
        if (!wx.getStorageSync('userId')) {
          return wx.navigateTo({ url: '/pages/index/impower/impower', })
        }
        wx.navigateTo({
          url: '/pages/index/youth/youth',
        })
        break;
      case "8":
        console.log('视频学习');
        wx.navigateTo({
          url: '/pages/videos/videos',
        })
        break;
    }
    // wx.navigateTo({ url: path })
  },
  // 轮播图
  getImgList() {
    console.log("---------首页轮播接口函数----------");
    A.updata.getImgList().then(res => {
      console.log(res);
      this.setData({ swiperImg: res.results })
    })
  },
  // 公告
  getNotices() {
    let that = this;
    if (this.data.getNotice == true) {
      this.noticeOfTheScroll();
      return;
    }
    A.updata.getNotices().then(res => {
      console.log('公告');
      console.log(res.results);
      if (res.results.length > 0) {
        let noticeContent = [];
        for (let i = 0; i < res.results.length; i++) {
          noticeContent.push(res.results[i].title);
        }
        that.setData({
          text: res.results,
          noNotice: true,
          getNotice: true
        })
        wx.createSelectorQuery().selectAll(".notice-list").boundingClientRect(rect => {
          that.setData({
            noticeWidth: rect[0].width
          });
          this.noticeOfTheScroll();
        }).exec();
      }
    })
  },

    cancelM:function(e){
        this.setData({
            showNoticeModal: true,
        })
    },

    noticeOfTheScroll() {
    let that = this;
    interval = setInterval(function () {
      // 判断初始位置是否小于view的宽度
      if (-that.data.marqueeDistance < that.data.noticeWidth) {
        that.setData({
          marqueeDistance: that.data.marqueeDistance - that.data.marqueePace,
        });
      } else {
        clearInterval(interval);
        that.setData({
          marqueeDistance: 320
        });
        that.noticeOfTheScroll();
      }
    }, that.data.interval)
  },
  viewNotices() {
    wx.showModal({
      title: '公告',
      content: '这是一个公告弹窗',
      showCancel: false
    })
  },
  //
  showNotice(e) {
    let content = A.C(e).content;
    this.setData({
        noticeContent:content,
        showNoticeModal:false
    })
    /*wx.showModal({
      title: '公告',
      showCancel: false,
      content: content,
    })*/
  },
  copyText(link) {
    // let content = A.C(e).content;
    wx.showModal({
      title: '提示',
      showCancel: false,
      content: "复制到浏览器打开",
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.setClipboardData({
            data: link,
            success: function (res) {
              wx.getClipboardData({
                success: function (res) {
                  wx.showToast({
                    title: '复制成功'
                  })
                }
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 每日学习
  getEveryDayStudy() {
    A.updata.getEveryDayStudy().then(res => {
      console.log(res);
      if(res.results == null) {
        console.log("每日学习");
        this.setData({
          noEveryDayStudy: false
        })
      }
      console.log(this.data.noEveryDayStudy);
      this.setData({ everyDayStudy: res.results })
    })
  },
  // 跳转每日学习页面
  toEveryDayStudy(e) {
    let id = A.C(e).id;
    console.log(`每日学习：${id}`);
    wx.navigateTo({
      url: `/pages/index/article/article?id=${id}&flag=0`,
    })
  },
  // 跳转搜索页面
  toSearch() {
    wx.navigateTo({
      url: '/pages/index/search/search',
    })
  },


  toRelationPage(e){
    let id = e.currentTarget.dataset.id;
    let type = e.currentTarget.dataset.type;
    if (type == 1) {
        wx.navigateTo({
            url: `/pages/mall/goodsDetail/shopInfo/shopInfo?id=${id}`
        })
    } else if (type == 2) {
        wx.navigateTo({
            url: `/pages/videos/videoDetail/videoDetail?id=${id}`
        })
    } else if (type == 3) {
        wx.navigateTo({
            url: `/pages/index/localLives/localLivesInfo/localLivesInfo?id=${id}`
        })
    } else if (type == 4) {
        wx.navigateTo({
            url: `/pages/mall/goodsDetail/shopInfo/shopInfo?id=${id}`
        })
    } else if (type == 5) {
        wx.navigateTo({
            url: `/pages/index/article/article?id=${id}&flag=1`
        })
    } else if (type == 6) {
        wx.navigateTo({
            url: `/pages/index/article/article?id=${id}&flag=1`
        })
    }
  },

  // 获取青年大学习信息
  getYouthLearn() {
    if (!wx.getStorageSync('userId')) {
      return wx.navigateTo({ url: '/pages/index/impower/impower', })
    }
    A.updata.getYouthLearn().then(res => {
      console.log(res);
      if(res.code == 200) {
        if (res.results.showRegisterBoxFlag == true) {
          wx.navigateTo({
            url: '/pages/index/bindOrgName/bindOrgName'
          })
        }else {
          this.copyText(res.results.learnLink);
        }
      } else {
        wx.showToast({
          title: `${res.msg}`,
          icon: 'none',
          duration: 1500
        })
      }
    })
  },

  // 字典
  getdictData() {
    let config = {
      dictData: 'region_type'
    }
    A.updata.getdictData(config).then(res => {
      console.log(res);
    })
  },

  // 签到
  signIn() {
    if (!wx.getStorageSync('userId')) {
      return wx.navigateTo({ url: '/pages/index/impower/impower', })
    }
    let config = {
      origin: 'SIGN_IN'
    }
    A.updata.addIntegraHistory(config).then(res => {
      console.log('获取积分');
      console.log(res);
      wx.showToast({
        title: '签到成功',
        icon: 'success',
        duration: 2000
      })
      this.setData({
        hasSign: true
      })
    })
  },
  login() {
    if (!wx.getStorageSync('userId')) {
      return;
    }
    let userInfo = JSON.parse(wx.getStorageSync('userInfo'));
    console.log(userInfo);
    let config = {
      avatar: userInfo.avatar,
      gender: userInfo.gender,
      nickname: userInfo.nickname,
      openId: userInfo.openId
    }
    A.updata.getLogin(config).then(res => {
      console.log(res);
      this.setData({
        hasSign: res.results.hasSign
      })
    })
  },
  onLoad() {
    if (!wx.getStorageSync('userId')) {
      wx.navigateTo({url: '/pages/index/impower/impower'})
    }
    this.getImgList();
    this.getEveryDayStudy();
    this.login();
    // this.getAritles();
    // this.getdictData();
    
  },
  onShow() {

    this.getNotices();
  },
  onHide() {

    clearInterval(interval);
  },
  onUnload() {
    clearInterval(interval);
  }
})
